<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cld:problem:add']">新增问题</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="problemList" border>
      <el-table-column label="所属主题" align="center" prop="degreetitleName" />
      <el-table-column label="问题名称" align="center" prop="degreetitleProblemName" />
      <el-table-column label="选项类型" align="center" prop="selectType" >
        <template slot-scope="scope">
          <dict-tag :options="selectTypeOptions" :value="scope.row.selectType" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:problem:edit']">修改</el-button>
          <el-button size="mini" type="text"  @click="handleDelete(scope.row)"
            v-hasPermi="['cld:problem:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改满意度问题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-form-item label="选择主题" prop="degreetitleId">
          <el-select v-model="form.degreetitleId" placeholder="请选择">
            <el-option
              v-for="item in titleList"
              :key="item.degreetitleId"
              :label="item.degreetitleName"
              :value="item.degreetitleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题名称" prop="degreetitleProblemName">
          <el-input v-model="form.degreetitleProblemName" placeholder="请输入问题名称" />
        </el-form-item>
        <!-- <el-form-item label="问题类型" prop="selectType">
          <el-select v-model="form.selectType" placeholder="请选择">
    <el-option
      v-for="item in selectTypeOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProblem, getProblem, delProblem, addProblem, updateProblem,getDegreeTitleList } from "@/api/cld/degreetitle";
import {selectTypeOptions} from '@/assets/staticDict'
export default {
  name: "Problem",
  props: {
    type: {
      type: Number
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 满意度问题表格数据
      problemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {
          degreetitleType: this.type
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        degreetitleId: [
          {required: true, message: "请选择主题", trigger: "change"}
        ],
        degreetitleProblemName: [
          {required: true, message: "问题名称不能为空", trigger: "blur"}
        ],
      },
      // 满意度主题
      titleList: [],
      // 问题类型
      selectTypeOptions: selectTypeOptions
    };
  },
  created() {
    this.getList();
    this.handleGetTheme();
  },
  methods: {
    /** 查询满意度问题列表 */
    getList() {
      this.loading = true;
      listProblem(this.queryParams).then(response => {
        this.problemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取满意度主题
    handleGetTheme(){
      getDegreeTitleList({
        degreetitleType: this.type
      }).then( res => {
        this.titleList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        degreetitleProblemId: null,
        degreetitleId: null,
        degreetitleProblemName: null,
        selectType: 1,
        createId: null,
        createName: null,
        createTime: null,
        flag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加满意度问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const degreetitleProblemId = row.degreetitleProblemId || this.ids
      getProblem(degreetitleProblemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改满意度问题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.degreetitleProblemId != null) {
            updateProblem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProblem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const degreetitleProblemIds = row.degreetitleProblemId || this.ids;
      this.$modal.confirm('是否确认删除满意度问题编号为"' + degreetitleProblemIds + '"的数据项？').then(function () {
        return delProblem(degreetitleProblemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
<style scoped>
.el-select{
  width: 100%;
}
</style>
